package top.lishuoboy.cloudstreamconsumer8802.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import top.lishuoboy.dependency.base.json.HuJsonUtil;

@Slf4j
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
    @Value("${server.port}")
    private String serverPort;

    @StreamListener(Sink.INPUT)
    public void input(Message<String> message) {
        /**
         * {
         *   "headers": {
         *     "amqp_consumerQueue": "studyExchange.anonymous.z0qwS5NNRk2Eu5SL8TEZJg",
         *     "amqp_consumerTag": "amq.ctag-pxG4ppQyJyyHH5RvD1Im4g",
         *     "amqp_deliveryTag": 1,
         *     "amqp_messageId": "fb01165b-2ab6-9067-216f-a5c9609ffd2c",
         *     "amqp_receivedDeliveryMode": "PERSISTENT",
         *     "amqp_receivedExchange": "studyExchange",
         *     "amqp_receivedRoutingKey": "studyExchange",
         *     "amqp_redelivered": false,
         *     "amqp_timestamp": 1651242653000,
         *     "contentType": {
         *       "parameters": {
         *       },
         *       "subtype": "json",
         *       "type": "application"
         *     },
         *     "deliveryAttempt": 1,
         *     "id": "39cb3809-e328-a44c-f0dd-8a8fd103ea14",
         *     "sourceData": {
         *       "body": [97,53,52,102,49,54,54,48,45,53,54,98,54,45,52,101,52,99,45,56,55,48,56,45,98,97,102,97,54,98,56,51,57,99,49,53],
         *       "messageProperties": {
         *         "appId": null,
         *         "clusterId": null,
         *         "consumerQueue": "studyExchange.anonymous.z0qwS5NNRk2Eu5SL8TEZJg",
         *         "consumerTag": "amq.ctag-pxG4ppQyJyyHH5RvD1Im4g",
         *         "contentEncoding": null,
         *         "contentLength": 0,
         *         "contentLengthSet": false,
         *         "contentType": "application/json",
         *         "correlationId": null,
         *         "deliveryMode": null,
         *         "deliveryTag": 1,
         *         "deliveryTagSet": true,
         *         "expiration": null,
         *         "finalRetryForMessageWithNoId": false,
         *         "headers": {
         *         },
         *         "lastInBatch": false,
         *         "messageCount": null,
         *         "messageId": "fb01165b-2ab6-9067-216f-a5c9609ffd2c",
         *         "priority": 0,
         *         "publishSequenceNumber": 0,
         *         "receivedDelay": null,
         *         "receivedDeliveryMode": "PERSISTENT",
         *         "receivedExchange": "studyExchange",
         *         "receivedRoutingKey": "studyExchange",
         *         "receivedUserId": null,
         *         "redelivered": false,
         *         "replyTo": null,
         *         "timestamp": 1651242653000,
         *         "type": null,
         *         "userId": null
         *       }
         *     },
         *     "timestamp": 1651242653122
         *   },
         *   "payload": "a54f1660-56b6-4e4c-8708-bafa6b839c15"
         * }
         */
        log.trace("{}", HuJsonUtil.toJsonStr(message));
        log.warn("消费者 port:{} 接受到的消息: {} ", serverPort, message.getPayload());
    }
}